package com.video4s.manager;

import org.apache.log4j.Logger;

import com.video4s.dao.VideoDAO;
import com.video4s.dao.VideoImplDAO;
import com.video4s.entity.VideoEntity;
import com.video4s.utils.PropetiesUtils;

public class ThreadQueueDBVideo implements Runnable {
	private static VideoDAO videoDAO = new VideoImplDAO();
	private static Logger logger = Logger.getLogger(ThreadQueueDBVideo.class);
	private static int SIZE_DB_VIDEO = 100;
	private static int SLEEP_DB_VIDEO = 10000;
	public ThreadQueueDBVideo() {
		logger.info("## START THREAD DB VIDEO ###############################################");
	}
	@Override
	public void run() {
		while(true) {
			try {
				SIZE_DB_VIDEO = Integer.parseInt(PropetiesUtils.loadValue("SIZE_DB_VIDEO"));
				SLEEP_DB_VIDEO = Integer.parseInt(PropetiesUtils.loadValue("SLEEP_DB_VIDEO"));
			} catch (Exception e) {
				logger.info("## ERROR LOAD CONFIG",e);
			}
			try {
				int queueSize = ManagerQueue.concurrentLinkedQueueVideo.size() > SIZE_DB_VIDEO ? SIZE_DB_VIDEO : ManagerQueue.concurrentLinkedQueueVideo.size();
				for (int i=0;i< queueSize;i++) {
					VideoEntity entity = ManagerQueue.concurrentLinkedQueueVideo.poll();
					//logger.info("## INSERT YOUTUBE: "+entity.getVideoFile());
					videoDAO.onInsert(entity);
				}
				Thread.sleep(SLEEP_DB_VIDEO);
			} catch (Exception e) {
				logger.info("## ERROR INSERT YOUTUBE: ",e);
			}
		}
	}

}
